<style lang="less">
    @import './person.less';
</style>

<template>
    <div>
        <Card>
            <p slot="title">
                <Icon type="person"></Icon>
                个人信息
            </p>
            <div>
                <Form 
                    ref="userForm"
                    :model="userForm" 
                    :label-width="100" 
                    label-position="right"
                    :rules="inforValidate"
                >
                    <FormItem label="项目名：">
                        {{userForm.projectname}}
                    </FormItem>
                    <FormItem label="用户名：">
                        {{userForm.username}}
                    </FormItem>
                    <FormItem label="公司：">
                        <span>国家电网公司</span>
                    </FormItem>
                    <FormItem label="密码：">
                        <Button type="primary" style="width: 100px;" :loading="save_loading" @click="showEditPassword">修改密码</Button>
                    </FormItem>
                </Form>
            </div>
        </Card>
        <Modal v-model="editPasswordModal" :closable='false' :mask-closable=false :width="500">
            <h3 slot="header" style="color:#2D8CF0">修改密码</h3>
            <Form ref="editPasswordForm" :model="editPasswordForm" :label-width="100" label-position="right" :rules="passwordValidate">
                <FormItem label="原密码" prop="oldPass" :error="oldPassError">
                    <Input v-model="editPasswordForm.oldPass" placeholder="请输入现在使用的密码" ></Input>
                </FormItem>
                <FormItem label="新密码" prop="newPass">
                    <Input v-model="editPasswordForm.newPass" placeholder="请输入新密码，至少6位字符" ></Input>
                </FormItem>
                <FormItem label="确认新密码" prop="rePass">
                    <Input v-model="editPasswordForm.rePass" placeholder="请再次输入新密码" ></Input>
                </FormItem>
            </Form>
            <div slot="footer">
                <Button type="text" @click="cancelEditPass">取消</Button>
                <Button type="primary" :loading="savePassLoading" @click="saveEditPass">保存</Button>
            </div>
        </Modal>
    </div>
</template>

<script>
export default {
    name: 'person',
    data () {
        const valideRePassword = (rule, value, callback) => {
            if (value !== this.editPasswordForm.newPass) {
                callback(new Error('两次输入密码不一致'));
            } else {
                callback();
            }
        };
        return {
            savePassLoading:false,
            editPasswordModal:false,
            userForm: {
                username: '',
                userid: '',
                projectname: ''
            },
            editPasswordForm: {
                oldPass: '',
                newPass: '',
                rePass: ''
            },
            passwordValidate: {
                oldPass: [
                    { required: true, message: '请输入原密码', trigger: 'blur' }
                ],
                newPass: [
                    { required: true, message: '请输入新密码', trigger: 'blur' },
                    { min: 6, message: '请至少输入6个字符', trigger: 'blur' },
                    { max: 32, message: '最多输入32个字符', trigger: 'blur' }
                ],
                rePass: [
                    { required: true, message: '请再次输入新密码', trigger: 'blur' },
                    { validator: valideRePassword, trigger: 'blur' }
                ]
            }
        };
    },
    methods: {
        showEditPassword () {
            this.editPasswordModal = true;
        },
        cancelEditPass () {
            this.editPasswordModal = false;
        },
        saveEditPass () {
            let self = this;
            this.$refs['editPasswordForm'].validate((valid) => {
                if (valid) {
                    self.savePassLoading = true;
                    self.$axios.post(self.$util.server+'manage/user/changepwd?userid='+self.userForm.userid+'&password='+self.editPasswordForm.oldPass+'&newpassword='+self.editPasswordForm.newPass, {}, {
                        headers: {
                            "Content-Type":"application/json;charset=utf-8"
                        },
                    }).then(function(response) {
                        console.log(response)
                        self.savePassLoading = false;
                        if(response.data.ok){
                            self.$Message.success({content: '密码修改成功',duration: 2})
                            self.cancelEditPass();
                        }else{
                            self.$Message.error({content: '操作失败：'+response.data.msg,duration: 3})
                        }
                    }).catch( function(response) {
                        self.savePassLoading = false;
                        console.log(response)
                    });
                }
            });
        },
        init () {
            this.userForm.username = localStorage.getItem('user')//用户名
            this.userForm.userid = localStorage.getItem('userid')//用户ID
            this.userForm.projectname = localStorage.getItem('projectname')//项目名
        }
    },
    mounted () {
        this.init();
    }
};
</script>